package cn.dengta.webapp.user.model;

import java.util.Map;
import java.util.function.Supplier;

import me.codeplayer.util.X;

public class MailContextTemplate {

	protected final MailTemplate mailTemplate;
	protected final transient Map<String, Object> context;

	protected Supplier<String> supplier;

	public MailContextTemplate(MailTemplate mailTemplate, Map<String, Object> context) {
		this.mailTemplate = mailTemplate;
		this.context = context;
	}

	public String getValue() {
		Supplier<String> sup = supplier;
		if (sup == null) {
			supplier = sup = () -> X.isValid(context) ? mailTemplate.parse(context) : mailTemplate.getTemplate();
		}
		return sup.get();
	}

}
